package com.bbcare.followup.plat.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.PropertiesUtils;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.SeqUtil;
import com.bbcare.followup.plat.entity.QuestionData;
import com.bbcare.followup.plat.service.impl.AppPatientAotuService;

/**
 * @desc app用户孕妇饮食，运动计划控制类
 * @author wuyz
 *
 */
@Controller
@RequestMapping("/webapi/service")
public class AppPatientAotuController {
	private final Log logger = LogFactory.getLog(AppPatientAotuController.class);
	@Autowired
	private AppPatientAotuService AppPatientAotuService;

	// 线上随访初始化服务
	@ResponseBody
	@RequestMapping("/sPatientFollowInit")
	public ResultModel<Map<String, Object>> sPatientFollowInit(@RequestBody String arg) throws Exception {
		logger.warn("in qryPatientByPhone----arg==" + arg);
		arg = URLDecoder.decode(arg, "utf-8");
		logger.warn("in 解码---arg==" + arg);
		JSONObject jsonArg = null;
		try {
			jsonArg = JSON.parseObject(arg);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
		}

		JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
		if (null == jsonData) {
			// 前台必须传data节点，否则报错返回
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
		}

		/* channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆 */
		String channel = jsonData.getString("channel");
		if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
		}

		String phoneNo = jsonData.getString("phoneNo");
		if (null == phoneNo || "".equals(phoneNo)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入服务号码或值为空！"));
		}
		String userName = jsonData.getString("userName");
		if (null == userName || "".equals(userName)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入用户姓名或值为空！"));
		}

		HashMap<String, Object> servParamMap = new HashMap<String, Object>();
		servParamMap.put("phoneNo", phoneNo);
		servParamMap.put("userName", userName);

		if (!StringUtils.isEmpty(jsonData.getString("followQueueId"))) {
			servParamMap.put("queueId", jsonData.getString("followQueueId"));
		}

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap = AppPatientAotuService.PatientFollowInit(servParamMap);
		return new ResultModel<Map<String, Object>>(retMap);
	}

	// 血糖记录提交服务
	@ResponseBody
	@RequestMapping("/bloodSugarCommint")
	public ResultModel<Map<String, Object>> bloodSugarCommint(@RequestBody String arg) throws Exception {
		logger.warn("in qryPatientByPhone----arg==" + arg);
		/*//以utf-8格式解码
		arg = URLDecoder.decode(arg, "utf-8");
		logger.warn("in 解码---arg==" + arg);*/

		JSONObject jsonArg = null;
		try {
			jsonArg = JSON.parseObject(arg);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
		}

		JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
		if (null == jsonData) {
			// 前台必须传data节点，否则报错返回
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
		}

		/* channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆 */
		String channel = jsonData.getString("channel");
		if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
		}

		String type = jsonData.getString("type");
		if (null == type || "".equals(type)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入操作类型或值为空！"));
		}

		String phoneNo = jsonData.getString("phoneNo");
		if (null == phoneNo || "".equals(phoneNo)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入服务号码或值为空！"));
		}

		Map<String, Object> quesMap = (JSONObject) jsonData.getJSONObject("questiones");
		if (null == quesMap || quesMap.size() <= 0) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入问卷信息或值为空！"));
		}
		logger.info("jsonData.getString111"+jsonData.getString("ptSchemeId"));
		String ptSchemeId = jsonData.getString("ptSchemeId");
		if (null == ptSchemeId || "".equals(ptSchemeId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参节点错误，未传入方案实例id或值为空！"));
		}

		String patientId = jsonData.getString("patientId");
		if (null == patientId || "".equals(patientId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入患者id或值为空！"));
		}

		String ptTaskId = jsonData.getString("ptTaskId");
		if (null == ptTaskId || "".equals(ptTaskId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
					"入参节点错误，未传入任务实例id或值为空！"));
		}

		String typeId = jsonData.getString("typeId");
		if (null == typeId || "".equals(typeId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
					"入参节点错误，未传入类型或值为空！"));
		}
		String struts = jsonData.getString("struts");
		if (null == struts || "".equals(struts)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入操作对象或值为空！"));
		}
		String opTime = jsonData.getString("opTime");
		if (StringUtils.isBlank(opTime)) {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
			opTime = df.format(new Date());// new Date()为获取当前系统时间
		}

		HashMap<String, Object> servParamMap = new HashMap<String, Object>();
		servParamMap.put("phoneNo", phoneNo);
		servParamMap.put("userId", patientId);
		servParamMap.put("ptTaskId", ptTaskId);
		servParamMap.put("ptSchemeId", ptSchemeId);
		servParamMap.put("questiones", quesMap);
		servParamMap.put("questions", (JSONObject) jsonData.getJSONObject("questiones"));
		servParamMap.put("type", type);
		servParamMap.put("typeId", typeId);
		servParamMap.put("struts", struts);
		servParamMap.put("opTime", opTime);
		// 通过服务号码和用户姓名查询userid
		if (!StringUtils.isEmpty(jsonData.getString("followQueueId"))) {
			servParamMap.put("queueId", jsonData.getString("followQueueId"));
		}

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap = AppPatientAotuService.bloodSugarCommint(servParamMap);
		return new ResultModel<Map<String, Object>>(retMap);
	}

	// 查询最近填写问卷次数控制类
	@ResponseBody
	@RequestMapping("/selectQusetionslately")
	public ResultModel<List<QuestionData>> selectQusetions(@RequestBody String arg) throws Exception {
		logger.warn("in qryPatientByPhone----arg==" + arg);
		// 解决乱码问题，需要调用端进行encode操作，若是前台调用需传utf-8格式
		arg = URLDecoder.decode(arg, "utf-8");
		logger.warn("in 解码---arg==" + arg);

		JSONObject jsonArg = null;
		try {
			jsonArg = JSON.parseObject(arg);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
		}

		JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
		if (null == jsonData) {
			// 前台必须传data节点，否则报错返回
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
		}
		String userId = jsonData.getString("userId");
		if (null == userId || "".equals(userId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入用户id或值为空！"));
		}

		/* channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆 */
		String channel = jsonData.getString("channel");
		if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
		}

		String schemeId = jsonData.getString("schemeId");
		if (null == schemeId || "".equals(schemeId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
					"入参节点错误，未传入队列实例id或值为空！"));
		}

		String taskId = jsonData.getString("taskId");
		if (null == taskId || "".equals(taskId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入任务id或值为空！"));
		}

		String typeId = jsonData.getString("typeId");
		if (null == typeId || "".equals(typeId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入类型或值为空！"));
		}

		String struts = jsonData.getString("struts");
		if (null == struts || "".equals(struts)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入用餐类型或值为空！"));
		}

		String code = jsonData.getString("code");
		if (StringUtils.isBlank(code)) {
			code = "6";
		}
		String opType = jsonData.getString("opType");

		HashMap<String, Object> servParamMap = new HashMap<String, Object>();
		servParamMap.put("schemeId", schemeId);
		servParamMap.put("taskId", taskId);
		servParamMap.put("typeId", typeId);
		servParamMap.put("struts", struts);
		servParamMap.put("code", code);
		servParamMap.put("opType", opType);
		servParamMap.put("userId", userId);
		if (!StringUtils.isEmpty(jsonData.getString("followQueueId"))) {
			servParamMap.put("queueId", jsonData.getString("followQueueId"));
		}

		List<QuestionData> retMap = AppPatientAotuService.selectQusetions(servParamMap);
		return new ResultModel<List<QuestionData>>(retMap);
	}

	// 修改运动计划，饮食计划
	@ResponseBody
	@RequestMapping("/changeExercise")
	public ResultModel<Map<String, Object>> changeExercise(@RequestBody String arg) throws Exception {
		logger.warn("in qryPatientByPhone----arg==" + arg);
		arg = URLDecoder.decode(arg, "utf-8");
		logger.warn("in 解码---arg==" + arg);

		JSONObject jsonArg = null;
		try {
			jsonArg = JSON.parseObject(arg);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
		}

		JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
		if (null == jsonData) {
			// 前台必须传data节点，否则报错返回
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
		}
		String userId = jsonData.getString("userId");
		if (null == userId || "".equals(userId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入用户id或值为空！"));
		}

		String schemeId = jsonData.getString("schemeId");
		if (null == schemeId || "".equals(schemeId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
					"入参节点错误，未传入队列实例id或值为空！"));
		}

		/* channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆 */
		String channel = jsonData.getString("channel");
		if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
		}

		String taskId = jsonData.getString("taskId");
		if (null == taskId || "".equals(taskId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "taskId入参节点错误，传入或值为空！"));
		}

		String typeId = jsonData.getString("typeId");
		if (null == typeId || "".equals(typeId)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "typeId入参节点错误，传入或值为空！"));
		}

		String struts = jsonData.getString("struts");
		if (null == struts || "".equals(struts)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "struts入参节点错误，未传入或值为空！"));
		}

		String code = jsonData.getString("opType");
		if (null == code || "".equals(code)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "opType入参节点错误，传入或值为空！"));
		}

		String questions = jsonData.getString("questions");
		if (null == questions || "".equals(questions)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
					"入参节点错误，未传入修改的json串或值为空！"));
		}

		String typeName = jsonData.getString("typeName");
		if (null == typeName || "".equals(typeName)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
					"入参节点错误，未传入问卷名称或值为空！"));
		}
		
		String oldId = jsonData.getString("oldId");

		HashMap<String, Object> servParamMap = new HashMap<String, Object>();
		servParamMap.put("userId", userId);
		servParamMap.put("schemeId", schemeId);
		servParamMap.put("taskId", taskId);
		servParamMap.put("typeId", typeId);
		servParamMap.put("struts", struts);
		servParamMap.put("code", code);
		servParamMap.put("questions", questions);
		servParamMap.put("typeName", typeName);
		servParamMap.put("oldId", oldId);
		servParamMap.put("followQueueId", jsonData.getString("followQueueId"));
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap = AppPatientAotuService.changeExercise(servParamMap);
		return new ResultModel<Map<String, Object>>(retMap);
	}

	// 查询能量信息
	@ResponseBody
	@RequestMapping("/queryEnergy")
	public ResultModel<List<Map<String, Object>>> queryEnergy(@RequestBody String arg) throws Exception {
		logger.warn("in qryPatientByPhone----arg==" + arg);
		arg = URLDecoder.decode(arg, "utf-8");
		logger.warn("in 解码---arg==" + arg);

		JSONObject jsonArg = null;
		try {
			jsonArg = JSON.parseObject(arg);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
		}

		JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
		if (null == jsonData) {
			// 前台必须传data节点，否则报错返回
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
		}
		String type = jsonData.getString("type");
		if (null == type || "".equals(type)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入查询类型或值为空！"));
		}

		/* channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆 */
		String channel = jsonData.getString("channel");
		if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
		}

		String fuzzyName = jsonData.getString("fuzzyName");
		if (StringUtils.isBlank(fuzzyName)) {
			fuzzyName = "";
		}

		Map<String, Object> servParamMap = new HashMap<String, Object>();
		servParamMap.put("type", type);
		servParamMap.put("fuzzyName", fuzzyName);
		servParamMap.put("enegry", fuzzyName);
		List<Map<String, Object>> list = AppPatientAotuService.selectEnegry(servParamMap);
		return new ResultModel<List<Map<String, Object>>>(list);
	}

	/**
	 * @name 食物和运动提交控制类
	 * @description:每次完成一项运动或者是吃完某一食物后选中时提交入库函数
	 * @author: wuyz
	 * @date: 2017年7月13日
	 * @version: 1.0
	 * @param
	 * @return success : true或者false
	 * 
	 **/
	@ResponseBody
	@RequestMapping("/foodExcierseSub")
	public ResultModel<Map<String, Object>> foodExcierseSub(@RequestBody String str) throws Exception {

		logger.info("foodExcierseSub~~~~~~~" + str);
		// 解决乱码问题，实现通过utf-8编码格式解码
		str = URLDecoder.decode(str, "utf-8");
		logger.info("解码后:" + str);

		// 定义入参接收json容器
		JSONObject jsonStr = null;
		// 将json字符串转换成json对象
		try {
			jsonStr = JSON.parseObject(str);
		} catch (Exception e) {
			e.printStackTrace();
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
		}

		// 取根节点下的入参
		JSONObject json = jsonStr.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

		// 判断是否有根节点data
		if (null == json) {
			// 前台必须传data节点，否则报错返回
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
		}

		/* channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆 */
		String channel = json.getString("channel");
		if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
		}
		
		//参数整合，判断
		String updateTime = DateUtil.getTimeString(DateUtil.getSysDateA());// 创建时间
		String updateAccept = SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID);// 操作流水
		
		String ptSchemeid = (String) json.getString("ptSchemeid");
		if (StringUtils.isBlank(ptSchemeid)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "ptSchemeid传入为空或者传入值不正确!");
		}
		
		String taskId = (String) json.getString("taskId");
		if (StringUtils.isBlank(taskId)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "taskId传入为空或者传入值不正确!");
		}
		
		String userId = (String) json.getString("userId");
		if (StringUtils.isBlank(userId)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "userId传入为空或者传入值不正确!");
		}
		
		String opType = (String) json.getString("opType");
		if (StringUtils.isBlank(opType)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "opType传入为空或者传入值不正确!");
		}
		
		String struts = (String) json.getString("struts");
		if (StringUtils.isBlank(struts)) {
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "struts传入为空或者传入值不正确!");
		}
		
		String questiones = json.getString("questiones");
		if (null == questiones || "".equals(questiones)) {
			throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
					"入参节点错误，未传入修改的json串或值为空！"));
		}
		//定义返回参数容器
		Map<String,Object> outMap = new HashMap<String,Object>();
		
		//定义调用业务逻辑层入参
		Map<String,Object> inMap = new HashMap<String,Object>();
		inMap.put("updateTime", updateTime);
		inMap.put("updateAccept", updateAccept);
		inMap.put("ptSchemeid", ptSchemeid);
		inMap.put("taskId", taskId);
		inMap.put("userId", userId);
		inMap.put("opType", opType);
		inMap.put("struts", struts);
		String type = json.getString("typeId");
		if (!StringUtils.isBlank(type)) {
			inMap.put("type", type);
		}
		inMap.put("contentData", json.getJSONObject("questiones"));
		
		//调用业务逻辑层
		outMap = AppPatientAotuService.doSubfoodAndExcierse(inMap);
		
		return new ResultModel<Map<String,Object>>(outMap);
	}
	
	
	
		/**
		 * @name:查询当前问卷控制类
		 * @description:查询当前用户问卷
		 * @author: wuyz
		 * @date: 2017年7月14日
		 * @version: 1.0
		 * @param  userId 		 String：用户（患者）id
		 * 		   schemeId 	 String：方案实例id
		 * 		   taskId 		 String：任务id
		 * 		   struts 		 String：类型 0:血糖,1:饮食 2：运动
		 *         typeName 	 String：问卷名称
		 *         followQueueId String：队列id
		 * @return 
		 * 		   TempletData   String：问卷信息
		 * 
		 **/
		@ResponseBody
		@RequestMapping("/queryCurrentTmplet")
		public ResultModel<Map<String, Object>> queryCurrentTmplet(@RequestBody String arg) throws Exception {
			logger.warn("in queryCurrentTmplet----arg==" + arg);
			arg = URLDecoder.decode(arg, "utf-8");
			logger.warn("in 解码---arg==" + arg);

			JSONObject jsonArg = null;
			try {
				jsonArg = JSON.parseObject(arg);
			} catch (Exception ex) {
				ex.printStackTrace();
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参格式错误，非json格式！"));
			}

			JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
			if (null == jsonData) {
				// 前台必须传data节点，否则报错返回
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！"));
			}
			String userId = jsonData.getString("userId");
			if (null == userId || "".equals(userId)) {
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入用户id或值为空！"));
			}

			String schemeId = jsonData.getString("schemeId");
			if (null == schemeId || "".equals(schemeId)) {
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
						"入参节点错误，未传入队列实例id或值为空！"));
			}

			/* channel渠道类型必传字段app登陆时特殊处理标识，跳过登陆 */
			String channel = jsonData.getString("channel");
			if (null == channel || !PropertiesUtils.getProperty("channel").equals(channel)) {
				throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "channel传入为空或者传入值不正确!");
			}

			String taskId = jsonData.getString("taskId");
			if (null == taskId || "".equals(taskId)) {
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入任务id或值为空！"));
			}

			String typeId = jsonData.getString("typeId");

			String struts = jsonData.getString("struts");
			if (null == struts || "".equals(struts)) {
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，未传入问卷类型或值为空！"));
			}

			String typeName = jsonData.getString("typeName");
			if (null == typeName || "".equals(typeName)) {
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,
						"入参节点错误，未传入问卷名称或值为空！"));
			}

			//定义入参
			HashMap<String, Object> servParamMap = new HashMap<String, Object>();
			servParamMap.put("userId", userId);
			servParamMap.put("schemeId", schemeId);
			servParamMap.put("taskId", taskId);
			servParamMap.put("typeId", typeId);
			servParamMap.put("struts", struts);
			servParamMap.put("typeName", typeName);
			servParamMap.put("followQueueId", jsonData.getString("followQueueId"));
			Map<String, Object> retMap = new HashMap<String, Object>();
			//调用接口
			retMap = AppPatientAotuService.queryCurrentTmplet(servParamMap);
			return new ResultModel<Map<String, Object>>(retMap);
		}

}
